﻿/**************************************************************
 *
 * 唯一标识：a1d622f0-5b9f-45b2-bcb5-eb0e4c2f151d
 * 命名空间：Sgr.Utilities
 * 创建时间：2023/7/24 9:50:54
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：Hash算法工具类，提供MD5和其他哈希算法的计算功能
 *
 *  string md5Hash = HashHelper.CreateMd5("Hello World");
 *  string sha1Hash = HashHelper.CreateSha1("Hello World");
 *  string sha256Hash = HashHelper.CreateSha256("Hello World");
 *  string sha384Hash = HashHelper.CreateSha384("Hello World");
 *  string sha512Hash = HashHelper.CreateSha512("Hello World");
 *
 *
 *
 *
 **************************************************************/

using System;
using System.Text;
using System.Text.Json;
using System.Security.Cryptography;

namespace Sgr.Utilities
{
    /// <summary>
    /// Hash工具类
    /// </summary>
    public static class HashHelper
    {
        #region MD5 哈希算法

        /// <summary>
        /// 获取MD5值
        /// </summary>
        /// <param name="inputValue">输入字符串</param>
        /// <returns>32位MD5字符串（大写）</returns>
        public static string CreateMd5(string inputValue)
        {
            if (string.IsNullOrEmpty(inputValue))
                return string.Empty;

            return CreateMd5(Encoding.UTF8.GetBytes(inputValue));
        }

        /// <summary>
        /// 获取对象的MD5值
        /// </summary>
        /// <param name="inputValue">输入对象</param>
        /// <returns>32位MD5字符串（大写）</returns>
        public static string CreateMd5(object inputValue)
        {
            if (inputValue == null)
                return string.Empty;

            byte[] jsonBytes = JsonSerializer.SerializeToUtf8Bytes(inputValue);
            return CreateMd5(jsonBytes);
        }

        /// <summary>
        /// 获取字节数组的MD5值
        /// </summary>
        /// <param name="buff">输入字节数组</param>
        /// <returns>32位MD5字符串（大写）</returns>
        public static string CreateMd5(byte[] buff)
        {
            Check.NotNull(buff, nameof(buff));

            var hashBytes = MD5.HashData(buff);
            return Convert.ToHexString(hashBytes);
        }

        #endregion MD5 哈希算法

        #region SHA1 哈希算法

        /// <summary>
        /// 计算SHA1哈希值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>40位SHA1字符串（大写）</returns>
        public static string CreateSha1(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return CreateSha1(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// 计算SHA1哈希值
        /// </summary>
        /// <param name="input">输入字节数组</param>
        /// <returns>40位SHA1字符串（大写）</returns>
        public static string CreateSha1(byte[] input)
        {
            Check.NotNull(input, nameof(input));

            var hashBytes = SHA1.HashData(input);
            return Convert.ToHexString(hashBytes);
        }

        #endregion SHA1 哈希算法

        #region SHA256 哈希算法

        /// <summary>
        /// 计算SHA256哈希值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>64位SHA256字符串（大写）</returns>
        public static string CreateSha256(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return CreateSha256(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// 计算SHA256哈希值
        /// </summary>
        /// <param name="input">输入字节数组</param>
        /// <returns>64位SHA256字符串（大写）</returns>
        public static string CreateSha256(byte[] input)
        {
            Check.NotNull(input, nameof(input));

            var hashBytes = SHA256.HashData(input);
            return Convert.ToHexString(hashBytes);
        }

        #endregion SHA256 哈希算法

        #region SHA384 哈希算法

        /// <summary>
        /// 计算SHA384哈希值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>96位SHA384字符串（大写）</returns>
        public static string CreateSha384(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return CreateSha384(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// 计算SHA384哈希值
        /// </summary>
        /// <param name="input">输入字节数组</param>
        /// <returns>96位SHA384字符串（大写）</returns>
        public static string CreateSha384(byte[] input)
        {
            Check.NotNull(input, nameof(input));

            var hashBytes = SHA384.HashData(input);
            return Convert.ToHexString(hashBytes);
        }

        #endregion SHA384 哈希算法

        #region SHA512 哈希算法

        /// <summary>
        /// 计算SHA512哈希值
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <returns>128位SHA512字符串（大写）</returns>
        public static string CreateSha512(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return CreateSha512(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// 计算SHA512哈希值
        /// </summary>
        /// <param name="input">输入字节数组</param>
        /// <returns>128位SHA512字符串（大写）</returns>
        public static string CreateSha512(byte[] input)
        {
            Check.NotNull(input, nameof(input));

            var hashBytes = SHA512.HashData(input);
            return Convert.ToHexString(hashBytes);
        }

        #endregion SHA512 哈希算法
    }
}